// pages/login/login.js
let app =  getApp();

Page({

    /**
     * Page initial data
     */
    data: {
        noAccount: true,//账户是否有输入
        noPasswd: true,//密码是否有输入
        tipAccount: '',//未输入账号提示信息
        tipPasswd: '',//未输入密码提示信息
        tipError:'',//账号密码输入错误提示信息
    },

    showAccount(e) {
        var that = this;
        that.setData({
            noAccount: false,
        });
    },

    showPasswd(e) {
        var that = this;
        that.setData({
            noPasswd: false,
        });
    },

    blurAccount(e) {
        var that = this;
        if(e.detail.value === '') { //没有输入
            that.setData({
                noAccount: true,
            })
        }
    },

    blurPasswd(e) {
        var that = this;
        if(e.detail.value === '') { //没有输入
            that.setData({
                noPasswd: true,
            })
        }
    },

    submit(e) {
        var that = this;
        var detail = e.detail.value;

        that.setData({//设置把提示信息消掉
            tipAccount: '',
            tipPasswd: '',
            tipError: '',
        })

        if(detail.account === undefined) {//没有输入账号
            that.setData({
                tipAccount: '请输入账号',
            })
        }

        if(detail.passwd === undefined) {//没有输入密码
            that.setData({
                tipPasswd: '请输入密码',
            })
        }

        if(detail.account !== undefined && detail.passwd !== undefined) {//成功输入账号和密码，鉴权开始
            wx.request({
                url: 'https://sduonline.cn/isduapi/api/auth/login/system',
                data: {
                    "u": detail.account,
                    "p": detail.passwd,
                },
                header: {'content-type':'application/x-www-form-urlencoded'},
                method: 'POST',
                success: (result) => {
                    if(result.data.code === 0) { //鉴权成功
                        wx.getUserProfile({//获取用户信息
                            desc: '显示昵称和头像', // 声明获取用户个人信息后的用途
                            success: (result) => {
                                wx.setStorageSync('userName', result.userInfo.nickName);//设置用户名缓存
                                wx.setStorageSync('avatarUrl', result.userInfo.avatarUrl);//设置头像缓存

                                //获取openid
                                wx.login({
                                    success: (result) => {
                                        var code = result.code;
                                        wx.request({
                                            url: app.globalData.urlHome + '/user/login',
                                            data: {
                                                "code": code,
                                            },
                                            header: {'content-type':'application/json'},
                                            method: 'POST',
                                            success: (result) => {
                                                wx.setStorageSync('openid', result.data);//设置openid缓存

                                                //查看用户是否已经在后端存在
                                                wx.request({
                                                    url: app.globalData.urlHome + '/user/one',
                                                    data: {
                                                        "openid": wx.getStorageSync('openid'),                                                          
                                                    },
                                                    header: {'content-type':'application/json'},
                                                    method: 'POST',
                                                    success: (result) => {
                                                        // console.log(result.data.length === 0);
                                                        if(result.data.length === 0) { //如果没有这个用户
                                                            //向后端请求创建一个用户
                                                            wx.request({
                                                                url: app.globalData.urlHome + '/user/add',
                                                                data: {
                                                                    "openid": wx.getStorageSync('openid'),
                                                                    "nickName": wx.getStorageSync('userName'),
                                                                    "avatarUrl": wx.getStorageSync('avatarUrl'),                                                                       
                                                                },
                                                                header: {'content-type':'application/json'},
                                                                method: 'POST',
                                                                success: (result) => {
                                                                    console.log(result);//打印出添加的用户信息
                                                                },
                                                                fail: () => {},
                                                                complete: () => {}
                                                            }); 
                                                        }
                                                    },
                                                    fail: () => {},
                                                    complete: () => {}
                                                });

                                                //登陆成功，重定向到首页
                                                wx.redirectTo({
                                                    url: './../head/head' + '?code=' + 0,
                                                });
                                                  
                                            },
                                            fail: () => {},
                                            complete: () => {}
                                        });
                                          
                                    },
                                    fail: () => {},
                                    complete: () => {}
                                });
                                    
                            }
                        });

                    }else { //鉴权失败
                        that.setData({
                            tipError:'账号或密码输入错误，请重新输入'
                        })
                    }
                },
                fail: (error) => {
                    console.log(error);
                },
                complete: () => {}
            });
              
        }
    },

    /**
     * Lifecycle function--Called when page load
     */
    onLoad: function (options) {
        //如果有该用户的登陆状态，则直接重定向到首页
        var openid = wx.getStorageSync('openid');
        if(openid !== '') {
            wx.redirectTo({
                url: './../head/head' + '?code=' + 1,
            });
              
        }
    },

    /**
     * Lifecycle function--Called when page is initially rendered
     */
    onReady: function () {

    },

    /**
     * Lifecycle function--Called when page show
     */
    onShow: function () {

    },

    /**
     * Lifecycle function--Called when page hide
     */
    onHide: function () {

    },

    /**
     * Lifecycle function--Called when page unload
     */
    onUnload: function () {

    },

    /**
     * Page event handler function--Called when user drop down
     */
    onPullDownRefresh: function () {

    },

    /**
     * Called when page reach bottom
     */
    onReachBottom: function () {

    },

    /**
     * Called when user click on the top right corner to share
     */
    onShareAppMessage: function () {

    }
})